package com.erik.crawler.service.crawler;

import com.erik.crawler.model.IpInfo;
import com.erik.crawler.service.ehcache.ProxyEhcache;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CrawlerJsoupImpl implements ICrawlerService {
    private static Logger LOGGER = LoggerFactory.getLogger(CrawlerJsoupImpl.class);

    @Autowired
    protected ProxyEhcache proxyEhcache;

    @Override
    public boolean isExistsSearch(String url) {
        boolean isExistsSearch = false;
        if (null == url) {
            return isExistsSearch;
        }

        IpInfo ipInfo = proxyEhcache.randomGetHighAvailableProxy();

        String html = null;
        if (null != ipInfo) {//is exists proxyIp
            html = getJsoupHtml(url, ipInfo.getIpStr(), ipInfo.getPort());
        } else {
            html = getJsoupHtml(url, null, null);
        }


        if (!StringUtil.isBlank(html)) {
            //build reg
            Pattern pattern = Pattern.compile("<input[^<>]+>*[@type='text']");
            //Build a matcher
            Matcher matcher = pattern.matcher(html);
            if (matcher.find()) {
                //exists
                isExistsSearch = true;
            }
        }

        return isExistsSearch;
    }

    /**
     * return  html
     *
     * @param url
     * @param ip
     * @param port
     * @return
     */
    private String getJsoupHtml(String url, String ip, String port) {

        if (null == url) {
            return null;
        }

        //return html value
        String html = null;

        try {
            if (!StringUtil.isBlank(ip) && !StringUtil.isBlank(port)) {//use proxyIp
                html = Jsoup.connect(url).timeout(5000).proxy(ip, Integer.parseInt(port)).get().html();
            } else {
                html = Jsoup.connect(url).timeout(5000).get().html();
            }
        } catch (IOException e) {
            LOGGER.info("this url : " + url, e.getMessage());
        }

        return html;
    }

}
